package com.tgky.ybzb.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tgky.ybzb.config.GetYml;
import com.tgky.ybzb.config.jwt.JwtUtil;
import com.tgky.ybzb.entity.Gcxq;
import com.tgky.ybzb.entity.SysDict;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.SysUser;
import com.tgky.ybzb.service.ISysUserService;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import com.tgky.ybzb.utils.MPUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tgky.ybzb.config.jwt.JwtUtil.getToken;

/**
 * 系统用户 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "系统用户", tags = "系统用户接口")
@RequestMapping("sys-user")
public class SysUserController {


    @Resource
    private ISysUserService sysUserService;

    @Resource
    private GetYml getYml;


    @PostMapping("/test")
    public Result test() {

        System.out.println(getYml.getSavePath());

        return Result.success(getYml.getSavePath());

    }


    /**
     * 用户登录
     *
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result userLogin(@RequestBody SysUser sysUser) {

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysUser::getUsername, sysUser.getUsername())
                .eq(SysUser::getPassword, DigestUtil.md5Hex(sysUser.getPassword()));

        SysUser user = sysUserService.getOne(queryWrapper);

        if (null == user) {
            return Result.error("用户名或密码错误！");
        }

        String token = getToken(user);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("sysUser", sysUser);
        resultMap.put("token", token);

        return Result.successData("登录成功。", resultMap);

    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入sysUser")
    public Result save(@RequestBody SysUser sysUser) {
        if (sysUser == null) {
            return Result.error("参数异常");
        }
        return Result.status(sysUserService.save(sysUser));
    }

    /**
     * 修改 系统用户
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入sysUser")
    public Result update(@RequestBody SysUser sysUser) {
        if (sysUser == null) {
            return Result.error("参数异常");
        }
        return Result.status(sysUserService.updateById(sysUser));
    }

    /**
     * 删除 系统用户
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }

        return Result.status(sysUserService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<SysUser> detail(Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        SysUser detail = sysUserService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 系统用户
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入sysUser")
    public Result list(PageParam pageParam, SysUser sysUser) {
        List<SysUser> list = sysUserService.list(sysUserService.getQueryWrapper(pageParam, sysUser));
        return Result.successData(list);
    }

    /**
     * 分页 系统用户
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入sysUser")
    public Result<IPage> page(PageParam pageParam, SysUser sysUser) {
        IPage<SysUser> pages = sysUserService.page(
                MPUtil.getPage(pageParam),
                sysUserService.getQueryWrapper(null, sysUser)
        );
        return Result.successData(pages);
    }

    /**
     * 系统字典表
     */
    @GetMapping("/dict")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询字典值")
    public Result<Map<String,List<SysDict>>> getDictList() {
        return Result.successData(sysUserService.getDictList());
    }

}
